import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { UrlService } from '../../../providers/url-service';
import { NoticeService } from '../../../providers/notice-service';
import { UserDetialChallengePage } from '../user-detial-challenge/user-detial-challenge';
import * as $ from "jquery";
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';

/**
 * 扫描二维码
 */
@Component({
  selector: 'page-user-sanner',
  templateUrl: 'user-sanner.html',
})
export class UserSannerPage {

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public noticeServe: NoticeService,
    private qrScanner: QRScanner,
    public urlService: UrlService

  ) {
    this.BcodeScanner();
  }

  ionViewDidLoad() {
    console.log('扫描二维码 UserSannerPage');
    $(".tabbar.show-tabbar").hide();
  
  }
  ionViewWillLeave() {
    $(".tabbar.show-tabbar").show();
  }
	/**
   * 条形码扫描器插件打开相机视图并自动扫描条形码，并将数据返回给您。
   */
  BcodeScanner() {
    this.qrScanner.prepare()
      .then((status: QRScannerStatus) => {
        if (status.authorized) {
          let scanSub = this.qrScanner.scan().subscribe((text: string) => {
            alert(text);

            //this.qrScanner.hide(); // hide camera preview
            scanSub.unsubscribe(); // stop scanning
          });
          this.qrScanner.show();

        } else if (status.denied) {
          //提醒用户没有开权限
        } else {

        }
      })
      .catch((e: any) => {
        this.noticeServe.showNoticeByToast(1, '扫描失败');
      });




  }
  getInfo(result) {
    this.urlService.GetbITMPUserInfo(result).then((data) => {
      if (data.Code == 0) {
        this.noticeServe.showNoticeByToast(1, '扫描成功');
        localStorage.setItem('personId', data.code);
        this.navCtrl.push(UserDetialChallengePage, { "userID": data.ID});
      } else {
        this.noticeServe.showNoticeByToast(2, '扫描失败');
        alert(JSON.stringify(data.Mag));
      }
    });
  }
}
